import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/order/MySeparatorVertical.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/SignUtil.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title, this.express_number, this.phone})
      : super(key: key);
  final String title;
  final String express_number;
  final String phone;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<String> dataList = [];
  List<String> time = [];
  String token;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      loadList();
    });
  }

  loadList() async {
    Map map = new Map();
    map['token'] = token;
    map['logistics'] = widget.title;
    map['phone'] = widget.phone;
    map['express_number'] = widget.express_number;
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/Kdn/getOrderTraces",
        data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    (userMap['data'] as List).forEach((v) {
      dataList.insert(0, v['AcceptStation']);
      time.insert(0, v['AcceptTime']);
    });
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('物流详情',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        elevation: 0.5,
        // actions: <Widget>[
        //   GestureDetector(
        //     child: Image.asset("images/icon_nav_share.png"),
        //   )
        // ],
      ),
      body: new SingleChildScrollView(
        child: Container(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                color: white,
                width: MediaQuery.of(context).size.width,
                padding: EdgeInsets.all(10),
                child: Text(
                  "快递公司: " + widget.title,
                  style: TextStyle(
                    fontSize: 14,
                  ),
                  softWrap: true,
                ),
              ),
              Container(
                height: 1,
              ),
                Container(
                color: white,
                width: MediaQuery.of(context).size.width,
                padding: EdgeInsets.all(10),
                child:Row(
                children: <Widget>[
                  GestureDetector(
                      onTap: () {
                        ClipboardData data =
                            new ClipboardData(text: widget.express_number);
                        Clipboard.setData(data);
                        Fluttertoast.showToast(
                            msg: "复制成功", gravity: ToastGravity.CENTER);
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Row(children: <Widget>[
                        Text(
                          "物流单号: " + widget.express_number,
                          style: TextStyle(
                            fontSize: 14,
                          ),
                          softWrap: true,
                        ),
                        Icon(Icons.content_copy, size: 15)
                      ])),
                ],
              ),
              ),
              Container(
                height: 5,
              ),
              _grayCenter(context)
            ],
          ),
        ),
      ),
    );
  }

  //灰色部分
  Widget _grayCenter(BuildContext context) {
    List<Widget> widgets = [];
    for (int i = 0; i < dataList.length; i++) {
      Widget widget = _getRow(dataList[i], time[i]);
      widgets.add(widget);
    }
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Color(0xFFF7F9FA),
        borderRadius: BorderRadius.circular(3),
      ),
      padding: EdgeInsets.only(left: 0, right: 15, top: 15, bottom: 15),
      child: Column(
        children: widgets,
      ),
    );
  }

  Widget _getRow(String followupListItem, String time) {
    String timestr = time;

    Widget firstRow;
    Widget contentWidget = SizedBox();
    //跟进记录
    contentWidget = Text(
      followupListItem ?? "--",
      style: TextStyle(
        fontSize: 12,
      ),
      softWrap: true,
    );

    firstRow = Row(
      children: <Widget>[
        Text(timestr,
            style: TextStyle(
              fontSize: 12.0,
            ))
      ],
    );

    Widget pointWidget;
    double topSpace = 0;
    topSpace = 3;
    pointWidget = ClipOval(
      child: Container(
        width: 7,
        height: 7,
        color: Colors.grey,
      ),
    );

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        //灰色右
        Expanded(
          child: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 37),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: topSpace == 0 ? 4 : 0,
                    ),
                    firstRow,
                    SizedBox(
                      height: 12.0,
                    ),
                    contentWidget,
                    SizedBox(
                      height: 12.0,
                    ),
                  ],
                ),
              ),
              Positioned(
                left: 0,
                width: 37,
                bottom: 0,
                top: topSpace,
                child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      pointWidget,
                      Expanded(
                        child: Container(
                          width: 27,
                          child: MySeparatorVertical(
                            color: Colors.grey,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}
